|
1
|
|
|
/** |
|
2
|
|
|
* Asynchronously save all settings in the current settings page to the database. |
|
3
|
|
|
* Shows an error notification if errors occur. Shows a success notification |
|
4
|
|
|
* otherwise. |
|
5
|
|
|
* |
|
6
|
|
|
* @param {Function} done |
|
7
|
|
|
*/ |
|
8
|
|
|
Amarkal.settings.save = function( done ) |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
Amarkal.settings._postData('save',function(res){ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
$('.amarkal-ui-component').amarkalUIComponent('reset'); |
|
13
|
|
|
console.log(res.errors); |
|
|
|
|
|
|
14
|
|
|
if(!$.isEmptyObject(res.errors)) { |
|
15
|
|
|
var error = '', i = 0; |
|
16
|
|
|
for(var name in res.errors) { |
|
|
|
|
|
|
17
|
|
|
if(i > 0) error += '<br>'; |
|
|
|
|
|
|
18
|
|
|
error += res.errors[name]; |
|
19
|
|
|
$('[amarkal-component-name="'+name+'"]').amarkalUIComponent('makeInvalid'); |
|
20
|
|
|
i++; |
|
21
|
|
|
} |
|
22
|
|
|
Amarkal.settings.notifier.error(error); |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
else { |
|
25
|
|
|
Amarkal.settings.notifier.success('Settings saved', 2000); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
Amarkal.settings._updateValues(res.values, res.errors); |
|
29
|
|
|
|
|
30
|
|
|
done(); |
|
31
|
|
|
}); |
|
32
|
|
|
}; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Asynchronously reset all settings in the current settings page to their |
|
36
|
|
|
* default values and erase all data from the database. Shows a success |
|
37
|
|
|
* notification upon completion. |
|
38
|
|
|
* |
|
39
|
|
|
* @param {Function} done |
|
40
|
|
|
*/ |
|
41
|
|
|
Amarkal.settings.reset = function( done ) |
|
42
|
|
|
{ |
|
43
|
|
|
Amarkal.settings._postData('reset',function(res){ |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
$('.amarkal-ui-component').amarkalUIComponent('reset'); |
|
46
|
|
|
|
|
47
|
|
|
Amarkal.settings.notifier.success('Default settings applied', 2000); |
|
|
|
|
|
|
48
|
|
|
Amarkal.settings._updateValues(res.values, res.errors); |
|
49
|
|
|
|
|
50
|
|
|
done(); |
|
51
|
|
|
}); |
|
52
|
|
|
}; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Update all components in the current settings page with the given values. |
|
56
|
|
|
* |
|
57
|
|
|
* @param {Object} values |
|
58
|
|
|
* @param {Array} errors |
|
59
|
|
|
*/ |
|
60
|
|
|
Amarkal.settings._updateValues = function( values, errors ) |
|
61
|
|
|
{ |
|
62
|
|
|
for(var name in values) { |
|
|
|
|
|
|
63
|
|
|
var value = values[name], |
|
64
|
|
|
$comp = $('[amarkal-component-name="'+name+'"]'); |
|
65
|
|
|
|
|
66
|
|
|
if( typeof errors !== 'undefined' && |
|
67
|
|
|
!errors.hasOwnProperty(name) && |
|
68
|
|
|
$comp.hasClass('amarkal-ui-component')) { |
|
69
|
|
|
|
|
70
|
|
|
$comp.amarkalUIComponent('setValue', value); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
}; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Send serialized form data to be processed in the backend by the function given |
|
77
|
|
|
* in the 'action' variable. |
|
78
|
|
|
* |
|
79
|
|
|
* @param {string} action |
|
80
|
|
|
* @param {Function} done |
|
81
|
|
|
*/ |
|
82
|
|
|
Amarkal.settings._postData = function( action, done ) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$.post(ajaxurl, { |
|
|
|
|
|
|
85
|
|
|
action: 'amarkal_settings_'+action, |
|
86
|
|
|
data: $('#amarkal-settings-form').serialize() |
|
87
|
|
|
}, done); |
|
88
|
|
|
}; |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.